﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EyFramework.Common;
using EyFramework.AutoCode.Entity;
using SqlSugar;

namespace EyFramework.AutoCode.DbFunc
{
    public partial class DbOraclePanel : UserControl, IDbPanel
    {
        private OracleDb _oracleDb = null;

        private bool _addFlag = false;
        public DbOraclePanel()
        {
            _addFlag = true;
            InitializeComponent();
        }
        public DbOraclePanel(OracleDb oraDb)
        {
            _oracleDb = oraDb;
            InitializeComponent();
            teConnectNm.Text = _oracleDb.ConnectName;
            teHostName.Text = _oracleDb.HostIp;
            tePort.Text = _oracleDb.Port;
            teServiceNm.Text = _oracleDb.ServiceSidName;
            teUserName.Text = _oracleDb.UserName;
            tePassword.Text = _oracleDb.Password;
        }
        /// <summary>
        /// 保存当前配置
        /// </summary>
        /// <returns></returns>
        public bool SaveDbConnect()
        {
            if (!CheckData())
            {
                return false;
            }
            FillData();
            if (_addFlag)
            {
                PublicGlobal.DataBases.OracleDbs.Add(_oracleDb);
            }
            return true;
        }

        private void FillData()
        {
            if (_addFlag)
            {
                _oracleDb = new OracleDb();
                _oracleDb.ConnectName = teConnectNm.Text.Trim();
                _oracleDb.HostIp = teHostName.Text.Trim();
                _oracleDb.Port = tePort.Text.Trim();
                _oracleDb.ServiceSidName = teServiceNm.Text.Trim();
                _oracleDb.UserName = teUserName.Text.Trim();
                _oracleDb.Password = tePassword.Text.Trim();
            }
            else
            {
                _oracleDb.ConnectName = teConnectNm.Text.Trim();
                _oracleDb.HostIp = teHostName.Text.Trim();
                _oracleDb.Port = tePort.Text.Trim();
                _oracleDb.ServiceSidName = teServiceNm.Text.Trim();
                _oracleDb.UserName = teUserName.Text.Trim();
                _oracleDb.Password = tePassword.Text.Trim();
            }
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(teConnectNm.Text.Trim()))
            {
                Globals.ShowWarning("连接名称不能为空");
                teConnectNm.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(teHostName.Text.Trim()))
            {
                Globals.ShowWarning("主机IP不能为空");
                teHostName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(tePort.Text.Trim()))
            {
                Globals.ShowWarning("端口不能为空");
                tePort.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(teServiceNm.Text.Trim()))
            {
                Globals.ShowWarning("服务名或SID不能为空");
                teServiceNm.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(teUserName.Text.Trim()))
            {
                Globals.ShowWarning("用户名不能为空");
                teUserName.Focus();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 测试连接
        /// </summary>
        /// <returns></returns>
        public bool TestConnect()
        {
            if (!CheckData())
            {
                return false;
            }

            #region 创建数据库连接
            string ConnectStr =
                "User Id={0};Password={1};Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={2})(PORT={3})))(CONNECT_DATA=(SERVICE_NAME={4})))";
            ConnectStr = string.Format(ConnectStr, teUserName.Text.Trim(),
                tePassword.Text.Trim(),
                teHostName.Text.Trim(),
                tePort.Text.Trim(),
                teServiceNm.Text.Trim());
            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                    {
                        ConnectionString = ConnectStr,//必填, 数据库连接字符串
                        DbType = SqlSugar.DbType.Oracle,        　//必填, 数据库类型
                        IsAutoCloseConnection = true       //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
                    }))
            {
                try
                {
                    db.Ado.CheckConnection();
                    return true;
                }
                catch (Exception ex)
                {
                    Globals.ShowError("数据库连接失败," + ex.ToString());
                    return false;
                }
            }
            #endregion
        }
    }
}
